﻿using DevelopAssistant.Core.DBMS;
using DevelopAssistant.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core.ToolBox
{
    public partial class TSQLEditorSettings : ToolBoxBase
    {
        public TSQLEditorSettings()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            this.comboBox1.Items.Add("Arial");
            this.comboBox1.Items.Add("Courier New");
            this.comboBox1.Items.Add("仿宋");
            this.comboBox1.Items.Add("宋体");
            this.comboBox1.Items.Add("黑体");
            this.comboBox1.Items.Add("微软雅黑");           

            int selectIndex=0;

            if (AppSettings.EditorSettings.EditorFont == null)
                return;

            var font = AppSettings.EditorSettings.EditorFont;
            foreach (string s in this.comboBox1.Items)
            {
                if (s.Equals(font.FontFamily.Name, StringComparison.InvariantCultureIgnoreCase))
                    break;

                selectIndex++;
            }

            this.comboBox1.SelectedIndex = selectIndex;
            this.textBox1.Text = font.Size.ToString();

            if (AppSettings.NewOpenQueryDocument)
            {
                radioButton1.Checked = true;
            }
            else
            {
                radioButton2.Checked = true;
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            AppSettings.EditorSettings.EnableDataBaseTran = this.checkBox1.Checked;
            AppSettings.EditorSettings.EnableIntellisense = this.checkBox2.Checked;
            AppSettings.EditorSettings.EditorFont = new Font(this.comboBox1.SelectedItem.ToString(), float.Parse(this.textBox1.Text));
                         
            MainForm mainForm =(MainForm) Application.OpenForms["MainForm"];
            if (mainForm != null)
            {
                var forms = mainForm.GetContents("QueryDocument");
                foreach (Form form in forms)
                {
                    if (form.GetType().Equals(typeof(DevelopAssistant.Core.DBMS.QueryDocument)))
                    {
                        ((QueryDocument)form).SetEditorFont();
                    }
                }
            }

            if (radioButton1.Checked)
            {
                AppSettings.NewOpenQueryDocument = true;
            }
            else
            {
                AppSettings.NewOpenQueryDocument = false;
            }             

            this.Close();
        }

        private void TSQLEditorSettings_Load(object sender, EventArgs e)
        {
            this.checkBox1.Checked = AppSettings.EditorSettings.EnableDataBaseTran;
            this.checkBox2.Checked = AppSettings.EditorSettings.EnableIntellisense;
        }
    }
}
